package com.aksu.business.controller.admin;


import com.aksu.server.dto.CategoryDto;
import com.aksu.server.dto.PageDto;
import com.aksu.server.dto.ResponseDto;
import com.aksu.server.service.CategoryService;
import com.aksu.server.util.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/admin/category")
public class CategoryController {

private static final Logger LOG = LoggerFactory.getLogger(CategoryController.class);
public static final String BUSINESS_NAME = "分类";

@Resource
private CategoryService categoryService;


/**
* 列表查询
*/
@PostMapping("/all")
public ResponseDto all() {
ResponseDto responseDto = new ResponseDto();
List<CategoryDto> categoryDtoList = categoryService.all();
responseDto.setContent(categoryDtoList);
return responseDto;
}

@PostMapping("/list")
public ResponseDto category(@RequestBody PageDto pageDto){
ResponseDto responseDto = new ResponseDto();
categoryService.list(pageDto);
responseDto.setContent(pageDto);
return responseDto;
}

@PostMapping("/save")
public ResponseDto save(@RequestBody CategoryDto categoryDto){

// 保存校验
            ValidatorUtil.require(categoryDto.getParent(), "父id");
            ValidatorUtil.require(categoryDto.getName(), "名称");
            ValidatorUtil.length(categoryDto.getName(), "名称", 1, 50);

ResponseDto responseDto = new ResponseDto();
categoryService.save(categoryDto);
responseDto.setContent(categoryDto);
return responseDto;
}

@DeleteMapping("/delete/{id}")
public ResponseDto delete(@PathVariable String id){
ResponseDto responseDto = new ResponseDto();
categoryService.delete(id);
return responseDto;
}
}
